package dao;



import bean.Goods;
import utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GoodsJDBCDao {

    public boolean add(Goods goods){
        Connection connection=null;
        PreparedStatement statement=null;
        try{
            connection=JDBCUtils.getConnection();
            String sql="insert into goods(id,name,price,count) values(?,?,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,"0005");
            statement.setString(2,"苹果12");
            statement.setDouble(3,2580.0);
            statement.setInt(4,12);
            int number=statement.executeUpdate();
            if(number>0){
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.release(statement,connection);
        }
        return false;
    }


    public Goods findById(String id){
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        Goods goods=new Goods();
        try{
            connection=JDBCUtils.getConnection();
            statement=connection.createStatement();
            String sql="select * from goods where id="+id;
            resultSet=statement.executeQuery(sql);
            while (resultSet.next()){
                goods.setId(resultSet.getString("id"));
                goods.setName(resultSet.getString("name"));
                goods.setPrice(resultSet.getDouble("price"));
                goods.setCount(resultSet.getInt("count"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.release(statement,connection,resultSet);
        }
        return goods;
    }

    public List<Goods> findByName(String name){
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        List<Goods> goodsList=new ArrayList<Goods>();
        try{
            connection=JDBCUtils.getConnection();
            statement=connection.createStatement();
            String sql="select * from goods where name like '"+name+"%'";
            resultSet=statement.executeQuery(sql);
            while (resultSet.next()){
                Goods goods=new Goods();
                goods.setId(resultSet.getString("id"));
                goods.setName(resultSet.getString("name"));
                goods.setPrice(resultSet.getDouble("price"));
                goods.setCount(resultSet.getInt("count"));
                goodsList.add(goods);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.release(statement,connection,resultSet);
        }
        return goodsList;
    }

//    public List<Goods> getAllGoodses(){
//        Connection connection=null;
//        Statement statement=null;
//        ResultSet resultSet=null;
//        List<Goods> goodsList=new ArrayList<Goods>();
//        try{
//            connection=JDBCUtils.getConnection();
//            statement=connection.createStatement();
//            String sql="select * from goods";
//            resultSet=statement.executeQuery(sql);
//            while (resultSet.next()){
//                Goods goods=new Goods();
//                goods.setId(resultSet.getString("id"));
//                goods.setName(resultSet.getString("name"));
//                goods.setPrice(resultSet.getDouble("price"));
//                goods.setCount(resultSet.getInt("count"));
//                goodsList.add(goods);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            JDBCUtils.release(statement,connection,resultSet);
//        }
//        return goodsList;
//    }


}
